﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using HomagGroup.Base.UI.Windows;
using HomagChina.Nesting.Views;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Threading;
using Telerik.Windows.Controls.GridView;
using Application = System.Windows.Application;
using ScrollViewer = System.Windows.Controls.ScrollViewer;
using HomagGroup.Base.UI.Themes.Resources;
using Keys = HomagGroup.Base.UI.Themes.Resources.Keys;

namespace HomagChina.Nesting
{
    public class PrintHistoryDialogViewModel : NotifyPropertyChangedBase
    {
        public IDictionary<int, Brush> CustomStatus { get; set; }
        private SearchInfoHandle Handle_Search;
        private DeleteInfoHandle Handle_Delete;
        private ModifyInfoHandle Handle_Modify;

        private ObservableCollection<T_Nesting_LabelTask> _List_PrintModel;

        public ObservableCollection<T_Nesting_LabelTask> List_PrintModel
        {
            get => _List_PrintModel;
            set { _List_PrintModel = value; OnPropertyChanged(); }
        }

        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }

        public RelayCommand HandlePrint { get; set; }

        public RelayCommand HandleDeletePrint { get; set; }
        private string _Reference;

        public string Reference
        { get => _Reference; set { _Reference = value; OnPropertyChanged(); } }

        private double _verticalOffset;

        public double VerticalOffset
        {
            get => _verticalOffset;
            set
            {
                if (_verticalOffset != value)
                {
                    _verticalOffset = value;
                    OnPropertyChanged();
                }
            }
        }

        private static DispatcherTimer timer;

        public PrintHistoryDialogViewModel()
        {
            Handle_Delete = new DeleteInfoHandle();
            Handle_Search = new SearchInfoHandle();
            LoadedCommand = new RelayCommand(ViewloadedCommand);
            UnLoadedCommand = new RelayCommand(ViewUnloadedCommand);
            HandlePrint = new RelayCommand(HandlePrintCommand);
            HandleDeletePrint = new RelayCommand(HandleDeletePrintCommand);

            CustomStatus = new Dictionary<int, Brush>
            {
                { 0, Application.Current.TryFindResource(Keys.MoreImage) as Brush },
                { 4, Application.Current.TryFindResource(InternalKeys.TableActualStateImage) as Brush },
                { 6, Application.Current.TryFindResource(InternalKeys.TableFinishedStateImage) as Brush }
            };

            timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(3000)
            };
        }

        private void HandleDeletePrintCommand(object obj)
        {
            if (HomagGroup.Base.UI.Windows.MessageBox.Show("Are you sure you want to add this recipe\r\n", "Prompt message", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                T_Nesting_LabelTask list = (T_Nesting_LabelTask)obj;
                if (Handle_Delete.DeletedPrintTask(list.Id))
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show("Congratulations. Deleted successfully!");
                    this.Refresh();
                }
            }
        }

        private void HandlePrintCommand(object obj)
        {
            //T_Nesting_LabelTask list = (T_Nesting_LabelTask)obj;
            //if (Handle_Modify.)
            //{
            //    HomagGroup.Base.UI.Windows.MessageBox.Show("Congratulations. Deleted successfully!");
            //    this.Refresh();
            //}
            HomagGroup.Base.UI.Windows.MessageBox.Show("Print job issued successfully");
        }

        private void ViewloadedCommand()
        {
            Refresh();
        }

        /// <summary>
        /// Unloaded
        /// </summary>
        private void ViewUnloadedCommand()
        {
            timer.Stop();
        }

        public event EventHandler RequestScrollPositionSave;

        public event EventHandler RequestScrollPositionRestore;

        public void Refresh()
        {
            var a = this;
            timer.Tick += (sender, args) =>
            {
                // 通知 View 保存滚动位置
                RequestScrollPositionSave?.Invoke(this, EventArgs.Empty);
                List_PrintModel = Handle_Search.GetPrintTasksList(Reference);
                // 通知 View 恢复滚动位置
                RequestScrollPositionRestore?.Invoke(this, EventArgs.Empty);
            };
            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                timer.Start();
            }
        }
    }
}